package com.higgins.eventtracking.client.projectmanagement;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HistoryTab extends ScrollPanel	{
	private final ProjectServiceAsync projectService = GWT
				.create(ProjectService.class);
	private VerticalPanel verticalPanel = new VerticalPanel();
	private TextBox dateTextBox = new TextBox();
	private CheckBox showAllEvents = new CheckBox();
	private CheckBox showDistinctList = new CheckBox();
	
	public HistoryTab()	{
		add(verticalPanel);
		setHeight("50em");
		setWidth("100em");
		populateDateTextBox();
		populateCheckBox();
		addHandler();
	}

	private void populateCheckBox()	{
		showAllEvents.setText("Show All Events");
		showAllEvents.setValue(false);
		showDistinctList.setText("Show Distinct List");
		showDistinctList.setValue(false);
	}
	private void populateDateTextBox()	{
		dateTextBox.setText("Loading....");
		projectService.retrieveTodaysSimpleFormatDate(new AsyncCallback<String>()    {
			@Override
			public void onFailure(Throwable caught)	{
			}
			@Override
			public void onSuccess(String results)	{
				dateTextBox.setText(results);
				executeHistoryTab(dateTextBox.getText(), showAllEvents.getValue(), showDistinctList.getValue()); 
			}
		});
	}
	private void textChanged()	{
		clearTab();
		executeHistoryTab(dateTextBox.getText(), showAllEvents.getValue(), showDistinctList.getValue());
	}
	private void clearTab()	{
		verticalPanel.clear();
	}
	
	private void executeHistoryTab(String dateStr, Boolean showAllEvents, Boolean showDistinctList)	{
		projectService.retrieveHistory(dateStr, showAllEvents, showDistinctList, new AsyncCallback<List<HistoryItemDTO>>()	{
			@Override
			public void onFailure(Throwable caught) {
			}
			@Override
			public void onSuccess(List<HistoryItemDTO> results) {
				populateHistoryTab(results);
			}
		});		
	}
	private void populateHistoryTab(List<HistoryItemDTO> results)	{
		verticalPanel.add(dateTextBox);
		verticalPanel.add(showAllEvents);
		verticalPanel.add(showDistinctList);
		verticalPanel.setCellHeight(dateTextBox, Integer.toString(dateTextBox.getElement().getOffsetHeight())+"px");
		for (HistoryItemDTO result:results)	{
			Label label = new Label(result.getDescription());
			verticalPanel.add(label);
			verticalPanel.setCellHeight(label, Integer.toString(label.getElement().getOffsetHeight())+"px");
		}
		verticalPanel.setHeight("100%");
//		setHeight("100%");
	}

	private void addHandler()	{
		class MyHandler implements KeyUpHandler, ValueChangeHandler {
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					textChanged();
				}
			}

			@Override
			public void onValueChange(ValueChangeEvent event) {
				textChanged();
			}
		}
		MyHandler myHandler = new MyHandler();
		dateTextBox.addKeyUpHandler(myHandler);
		showAllEvents.addValueChangeHandler(myHandler);
		showDistinctList.addValueChangeHandler(myHandler);
	}
	
	
}
